package casa.depesos.app.com.frag;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import casa.depesos.app.com.activity.BankActivity;
import casa.depesos.app.com.databinding.FragmentBankCodeBinding;
import casa.depesos.app.com.http.data.CodeType;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Utils;

public class BankCodeFragment extends SmsBaseFragment {

    FragmentBankCodeBinding binding;
    TextView tip;
    boolean got = false;

    public BankCodeFragment setParam(TextView tv) {
        tip = tv;
        return this;
    }

    public void hideTip() {
        if (tip != null) {
            //    tip.setVisibility(View.GONE);
        }
    }

    public void showTip() {
        if (tip != null && got) {
            tip.setVisibility(View.VISIBLE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentBankCodeBinding.inflate(inflater, container, false);
        Utils.setEditTextInputSpace(binding.pwd, 6);
        binding.pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                ((BankActivity) getActivity()).check();
            }
        });
        binding.get2.setOnClickListener(v -> getCode());

        return binding.getRoot();
    }

    public boolean validCode() {
        if (Constant.white) {
            return true;
        }
        return getSmsCode().length() == 6;
    }

    public String getSmsCode() {
        return binding.pwd.getText().toString().trim();
    }

    @Override
    protected void countDown() {
        super.countDown();
        got = true;
        binding.get2.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void timeOut() {
        binding.get2.setVisibility(View.VISIBLE);
    }

    @Override
    protected String getPhone() {
        return Utils.getPhone();
    }

    @Override
    protected CodeType getCodeType() {
        return CodeType.BIND;
    }

    @Override
    protected TextView getCodeTv() {
        return binding.get;
    }
}
